package com.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.*;

import com.st.mapper.IAttractionMapper;
import com.st.mapper.IScenicDetailMapper;
import com.st.mapper.ITicketMapper;
import com.st.service.IAttractionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
@Transactional
public class AttractionsServiceImpl extends ServiceImpl<IAttractionMapper, Attractions> implements IAttractionsService {
    @Autowired
    private IAttractionMapper attractionsMapper;
    @Autowired
    private ITicketMapper iTicketMapper;
    @Autowired
    private IScenicDetailMapper iScenicDetailMapper;

    @Override
    public List<Attractions> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Attractions> list = attractionsMapper.selectList(queryWrapper);
        List<Site> all = attractionsMapper.findAll();
        list.forEach(attractions -> {
            all.forEach(site -> {
                if (attractions.getProvince().equals(site.getPid().toString())) {
                    attractions.setProvince(site.getProvince());
                }
                if (site.getCid() == null || site.getAid() == null) {
                    site.setAid(1);
                    site.setArea("a");
                    site.setCid(2);
                    site.setCity("b");
                }
                if (attractions.getCity().equals(site.getCid().toString())) {
                    attractions.setCity(site.getCity());
                }
                if (attractions.getAreas().equals(site.getAid().toString())) {
                    attractions.setAreas(site.getArea());
                }
            });
        });
        return list;
    }

    @Override
    public PageResult<Attractions> findAttractions(PageDTO<Attractions> pageDTO) {
        //条件与分页
        if (pageDTO == null) {
            return new PageResult<Attractions>(false, "查询失败", null, null, null);
        }
        IPage<Attractions> iPage = new Page<>(pageDTO.getPage(), pageDTO.getLimit());
        Attractions data = pageDTO.getData();
        QueryWrapper<Attractions> queryWrapper = new QueryWrapper<>(pageDTO.getData());
        if (!StringUtils.isEmpty(data.getName())) {
            queryWrapper.eq("name", data.getName());
        }
        if (!StringUtils.isEmpty(data.getSite())) {
            queryWrapper.eq("site", data.getSite());
        }
        IPage<Attractions> pageInfo = attractionsMapper.findAttractions(iPage, pageDTO.getData());
        PageResult pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setData(pageInfo.getRecords());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setMsg("查询成功");
        return pageResult;

    }

    @Override
    public Result addAttraction(Attractions attractions) {
        if (attractions==null){
            return new Result(false,"添加失败");
        }
//        QueryWrapper<Attractions> queryWrapper = new QueryWrapper<>();
//        List<Attractions> attractions1 = attractionsMapper.selectList(queryWrapper);
//        attractions.setId(attractions1.get((attractions1.size())).getId());
//        ScenicDetail scenicDetail = new ScenicDetail();
//        scenicDetail.setName(attractions.getName());
//        scenicDetail.setSite(attractions.getSite());
//        scenicDetail.setAttractionsId(attractions.getId());
//        Ticket ticket = new Ticket();
//        ticket.setId(attractions.getId());
//        ticket.setName(attractions.getName());
          Date date = new Date();
          attractions.setDepartureData(date);
//        int k = iTicketMapper.insert(ticket);
//        int i = iScenicDetailMapper.insertScenicDetail(scenicDetail);
        int j = attractionsMapper.insert(attractions);
        if (j==1){
            return new Result(true,"添加成功");
        }else {
            return new Result(false,"添加失败");
        }
    }

    @Override
    public Result updateAttraction(Attractions attractions) {
        if (attractions==null){
            return new Result(false,"修改失败");
        }
        int i = attractionsMapper.updateById(attractions);
        ScenicDetail scenicDetail = new ScenicDetail();
        scenicDetail.setName(attractions.getName());
        scenicDetail.setSite(attractions.getSite());
        scenicDetail.setAttractionsId(attractions.getId());
        Ticket ticket = new Ticket();
        ticket.setAttractionsId(attractions.getId());
        ticket.setName(attractions.getName());
        QueryWrapper<Ticket> queryWrapper = new QueryWrapper<>();
        List<Ticket> tickets = iTicketMapper.selectList(queryWrapper);
        for (Ticket ticket1 : tickets) {
            if (ticket1.getAttractionsId()==attractions.getId()){
                iTicketMapper.updateTicket(ticket);
            }
        }
        QueryWrapper<ScenicDetail> queryWrapper1 = new QueryWrapper<>();
        List<ScenicDetail> scenicDetails = iScenicDetailMapper.selectList(queryWrapper1);
        for (ScenicDetail ticket1 : scenicDetails) {
            if (ticket1.getAttractionsId()==attractions.getId()){
                iScenicDetailMapper.updateScenicDetail(scenicDetail);
            }
        }
        if (i==1){
            return new Result(true,"修改成功");
        }else {
            return new Result(false,"修改失败");
        }
    }

    @Override
    public Result delAttraction(Attractions attractions) {
        int i = attractionsMapper.deleteById(attractions.getId());
        QueryWrapper<Ticket> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("attractions_id",attractions.getId());
        Ticket ticket = new Ticket();
        QueryWrapper<Ticket> queryWrapper2 = new QueryWrapper<>();
        List<Ticket> tickets = iTicketMapper.selectList(queryWrapper2);
        iTicketMapper.delete(queryWrapper1);
        QueryWrapper<ScenicDetail> queryWrapper3 = new QueryWrapper<>();
        List<ScenicDetail> scenicDetails = iScenicDetailMapper.selectList(queryWrapper3);
        QueryWrapper<ScenicDetail> queryWrapper4 = new QueryWrapper<>();
        queryWrapper3.eq("attractions_id",attractions.getId());
        iScenicDetailMapper.delete(queryWrapper3);
        if (i==1){
            return new Result(true,"删除成功");
        }else {
            return new Result(false,"删除失败");
        }
    }

    @Override
    public List<Attractions> findAll1() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<Attractions> list = attractionsMapper.selectList(queryWrapper);
        return list;
    }


}
